﻿using System;
using XNAGameEngine.Input;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using XNAGameEngine;
using XNAGameEngine.GameObject;
using XNAGameEngine.DataStructures;
using XNAGame;
using XNAGameEngine.Utility;


namespace XNAGameEngine
{
    public class WarriorQueue
    {

        private PriorityQueue<Warrior> TurnQueue;
        private PriorityQueue<Warrior> TempQueue;
        private Warrior ThisGuysTurn;
        public Warrior tempGuy;
        private int decrement;

        private int numOfDudesInQueue;

        public WarriorQueue()
        {
        }

        public void Initilize(int howManyDudes)
        {
            numOfDudesInQueue = howManyDudes;
            TurnQueue = new PriorityQueue<Warrior>();
            TempQueue = new PriorityQueue<Warrior>();
        }

        public void Update(GameTime gameTime)
        {
        }

        public void QueueGuysUp()
        {
            TurnQueue.Enqueue(tempGuy);

        }

        public Warrior makeMoves()
        {

            ThisGuysTurn = TurnQueue.Peek();
            decrement = ThisGuysTurn.currentspeed;
            TempQueue.Enqueue(TurnQueue.Peek());

            TurnQueue.Dequeue();

            return ThisGuysTurn;
        }

        public Warrior GetNextMove()
        {
            while (TurnQueue.Count != 0)
            {
                while (TurnQueue.Count != 0)
                {
                    TurnQueue.Peek().currentspeed -= decrement;
                    TempQueue.Enqueue(TurnQueue.Peek());
                    TurnQueue.Dequeue();
                }

                if (TempQueue.Count != 0 && TempQueue.Peek().currentspeed <= 0)
                {
                    tempGuy = TempQueue.Dequeue();
                    tempGuy.currentspeed = tempGuy.normalspeed;
                    TempQueue.Enqueue(tempGuy);
                }
            }

            while (TempQueue.Count != 0)
            {
                TurnQueue.Enqueue(TempQueue.Peek());
                TempQueue.Dequeue();
            }

            //Console.WriteLine("TurnQueue Count={0}", TurnQueue.Count);
            //Console.WriteLine("TempQueue Count={0}", TempQueue.Count);

            ThisGuysTurn = TurnQueue.Peek();
            decrement = TurnQueue.Peek().currentspeed;

            return ThisGuysTurn;
        }

        public void DestroyQueue()
        {
        }   
    }
}   
